﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CYTools.Util
{
    public class LogManager
    {
        private static string logPath = string.Empty;
        /// <summary>
        /// save log folder
        /// </summary>
        public static string LogPath
        {
            get
            {
                if (logPath == string.Empty)
                {
                    logPath = AppDomain.CurrentDomain.BaseDirectory + @"Log\";
                }
                return logPath;
            }
            set { logPath = value; }
        }

        private static string logFielPrefix = string.Empty;
        /// <summary>
        /// log file prefix
        /// </summary>
        public static string LogFielPrefix
        {
            get { return logFielPrefix; }
            set { logFielPrefix = value; }
        }

        /// <summary>
        /// write log
        /// </summary>
        public static void WriteLog(string logFile, string msg)
        {
            try
            {
                if (!Directory.Exists(LogPath))
                {
                    Directory.CreateDirectory(LogPath);
                }
                string FileName = LogPath + LogFielPrefix + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                if (!System.IO.File.Exists(FileName))
                {
                    FileStream tempfs = File.Create(FileName);
                    tempfs.Close();
                }
                System.IO.StreamWriter sw = System.IO.File.AppendText(FileName);
                StringBuilder sb = new StringBuilder();
                //sb.Append("---------------------------------------------------------------------\r\n");
                sb.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "  [" + logFile + "]  ");
                sb.Append(msg);
                sw.WriteLine(sb.ToString());
                sw.Flush();
                sw.Close();
            }
            catch (System.IO.IOException IoEx)
            {
                WriteLog(LogFile.ERROR, IoEx.ToString());
            }
        }
        /// <summary>
        /// write log
        /// </summary>
        public static void WriteLog(string logFile, string msg, string fileName)
        {
            try
            {
                if (!Directory.Exists(LogPath))
                {
                    Directory.CreateDirectory(LogPath);
                }
                string FileName = LogPath + LogFielPrefix + DateTime.Now.ToString("yyyyMMdd") + fileName + ".txt";
                if (!System.IO.File.Exists(FileName))
                {
                    FileStream tempfs = File.Create(FileName);
                    tempfs.Close();
                }
                System.IO.StreamWriter sw = System.IO.File.AppendText(FileName);

                StringBuilder sb = new StringBuilder();
                //sb.Append("---------------------------------------------------------------------\r\n");
                sb.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "  [" + logFile + "]  ");
                sb.Append(msg);
                sw.WriteLine(sb.ToString());
                sw.Flush();
                sw.Close();
            }
            catch (System.IO.IOException IoEx)
            {
                WriteLog(LogFile.ERROR, IoEx.ToString());
            }
        }
        /// <summary>
        /// write log
        /// </summary>
        public static void WriteLog(LogFile logFile, string msg)
        {
            WriteLog(logFile.ToString(), msg);
        }
        /// <summary>
        /// write log
        /// </summary>
        public static void WriteLog(LogFile logFile, string msg, string fileName)
        {
            WriteLog(logFile.ToString(), msg, fileName);
        }
    }

    /// <summary>
    /// log type
    /// </summary>
    public enum LogFile
    {
        TRACE,
        WARNING,
        ERROR,
        DEBUG,
        SQL,
        SYSTEM
    }
}
